/*
 * 文 件 名:  XMLReader.java
 * 版    权:  Chrhc Technologies Co., Ltd. Copyright 2015-2099,  All rights reserved
 * 描    述:  <描述>
 * 修 改 人: 姓名 xxx
 * 修改时间:  2015年11月23日
 */
package test.model;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
 * 
 * @author Martin3000
 * 
 */
public class ConfigXMLReader
{
    // 配置文件名
    // private static String filename = "conf.xml";
    
    /**
     * <一句话功能简述> <功能详细描述>
     * 
     * @param fileName
     * @return
     */
    public static Map getconfig(String fileName)
    {
        // Config config = new Config();
        Map config = new HashMap();
        try
        {
            File f = new File(fileName);
            if (!f.exists())
            {
                System.out.println("  Error : Config file doesn't exist!");
                System.exit(1);
            }
            SAXReader reader = new SAXReader();
            reader.setValidation(false);
            reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
            Document doc;
            doc = reader.read(f);
            
            Element root = doc.getRootElement();
            Element data;
            // Iterator<?> itr = root.elementIterator("context");
            
            Element context = root.element("context");
            System.out.println("context::" + context);
            Element jdbc = context.element("jdbcConnection");
            System.out.println("jdbc::" + jdbc);
            
            Attribute driverClass = jdbc.attribute("driverClass");
            System.out.println("driverClass::" + driverClass.getText());
            config.put("driverClass", driverClass.getText());
            Attribute connectionURL = jdbc.attribute("connectionURL");
            System.out.println("connectionURL::" + connectionURL.getText());
            config.put("connectionURL", connectionURL.getText());
            Attribute userId = jdbc.attribute("userId");
            System.out.println("userId::" + userId.getText());
            config.put("userId", userId.getText());
            Attribute password = jdbc.attribute("password");
            System.out.println("password::" + password.getText());
            config.put("password", password.getText());
            Element javaModelGenerator = context.element("javaModelGenerator");
            Attribute targetPackage = javaModelGenerator.attribute("targetPackage");
            System.out.println("targetPackage::" + targetPackage.getText());
            config.put("targetPackage", targetPackage.getText());
            Iterator it = context.elementIterator("table");
            List tableList = new ArrayList();
            while (it.hasNext())
            {
                Map tableMap = new HashMap();
                tableList.add(tableMap);
                data = (Element)it.next();
                Attribute tableName = data.attribute("tableName");
                System.out.println("tableName::" + tableName.getText());
                tableMap.put("tableName", tableName.getText());
                Attribute domainObjectName = data.attribute("domainObjectName");
                System.out.println("domainObjectName::" + domainObjectName.getText());
                tableMap.put("domainObjectName", domainObjectName.getText());
                putTableColumn(data, tableMap);
            }
            config.put("tableList", tableList);
        }
        catch (Exception ex)
        {
            System.out.println("Error : " + ex.toString());
        }
        return config;
        
    }
    
    private static void putTableColumn(Element table, Map tableMap)
    {
        Iterator it = table.elementIterator("colomn");
        Element col;
        
        Map colsMap = new HashMap();
        tableMap.put("colsMap", colsMap);
        Map colMap = null;
        while (it.hasNext())
        {
            colMap = new HashMap();
            col = (Element)it.next();
            Attribute colName = col.attribute("name");
            colMap.put("name", colName.getText());
            colsMap.put(colName.getText(), colMap);
            Iterator colIt = col.elementIterator("anno");
            List annList = new ArrayList();
            while (colIt.hasNext())
            {
                Element anno = (Element)colIt.next();
                annList.add(anno.getTextTrim());
            }
            colMap.put("annos", annList);
        }
        
    }
}
